package cn.tedu.luban.worker.main.converter;

import cn.tedu.luban.attach.protocol.param.AttachUpdateParam;
import cn.tedu.luban.passport.protocol.jwt.UserPrinciple;
import cn.tedu.luban.passport.sdk.security.LubanSecurityContext;
import cn.tedu.luban.worker.common.enums.AccountStatusEnum;
import cn.tedu.luban.worker.common.enums.AuditStatusEnum;
import cn.tedu.luban.worker.common.enums.StatusEnum;
import cn.tedu.luban.worker.protocol.VO.FrontWorkerAccountVO;
import cn.tedu.luban.worker.protocol.VO.FrontWorkerAreaVO;
import cn.tedu.luban.worker.protocol.VO.FrontWorkerCategoryVO;
import cn.tedu.luban.worker.protocol.VO.FrontWorkerDetailVO;
import cn.tedu.luban.worker.protocol.VO.AdminWorkerListVO;
import cn.tedu.luban.worker.protocol.bo.FrontAccountBO;
import cn.tedu.luban.worker.protocol.bo.WorkerAreaBO;
import cn.tedu.luban.worker.protocol.bo.AdminWorkerBO;
import cn.tedu.luban.worker.protocol.bo.FrontWorkerBO;
import cn.tedu.luban.worker.protocol.bo.WorkerCategoryBO;
import cn.tedu.luban.worker.protocol.dos.WorkerDO;
import cn.tedu.luban.worker.protocol.param.IdCardParam;
import cn.tedu.luban.worker.protocol.param.WorkerRegistParam;
import cn.tedu.luban.worker.protocol.param.WorkerParam;
import cn.tedu.luban.worker.protocol.query.WorkerDBPageQuery;
import cn.tedu.luban.worker.protocol.query.WorkerListQuery;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.pager.PagerResult;
import com.tedu.inn.protocol.pager.SimplePagerQuery;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Component
public class WorkerConverter {
    public List<AttachUpdateParam> assembleAttachParam(List<IdCardParam> idCardParams,Integer bizType,Integer bizId){
        if (CollectionUtils.isEmpty(idCardParams)){
            return null;
        }
        List<AttachUpdateParam> attachUpdateParams=new ArrayList<>();
        for (IdCardParam idCardParam : idCardParams) {
            AttachUpdateParam attachUpdateParam=new AttachUpdateParam();
            attachUpdateParam.setBusinessType(bizType);
            attachUpdateParam.setBusinessId(bizId);
            attachUpdateParam.setId(idCardParam.getId());
            attachUpdateParam.setIsCover(0);
            attachUpdateParams.add(attachUpdateParam);
        }
        return attachUpdateParams;
    }
    public WorkerDO param2do(WorkerRegistParam param) {
        WorkerDO worker = new WorkerDO();
        if (param==null){
            return new WorkerDO();
        }
        BeanUtils.copyProperties(param, worker);
        UserPrinciple loginUser = LubanSecurityContext.getLoginToken();
        worker.setGmtCreate(System.currentTimeMillis());
        worker.setGmtModified(worker.getGmtCreate());
        worker.setCreateUserId(loginUser != null ? loginUser.getId() : 0);
        worker.setModifiedUserId(loginUser != null ? loginUser.getId() : 0);
        worker.setCreateUserName(loginUser != null ? loginUser.getUsername() : "mock");
        worker.setModifiedUserName(loginUser != null ? loginUser.getUsername() : "mock");
        //默认是正常的
        worker.setStatus(StatusEnum.NORMAL.getValue());
        //默认未审核
        worker.setAuditStatus(AuditStatusEnum.UNAUDITED.getValue());
        //默认未启用
        worker.setCertStatus(AccountStatusEnum.DISABLE.getValue());
        return worker;
    }
    public FrontWorkerDetailVO bo2vo(FrontWorkerBO bo) {
        FrontWorkerDetailVO workerDetailVO = new FrontWorkerDetailVO();
        workerDetailVO.setAreaList(areaBos2AreaVos(bo.getWorkerAreaBOList()));
        workerDetailVO.setCategoryList(catsBos2catesVos(bo.getWorkerCategoryBOList()));
        workerDetailVO.setAccountVO(bo2vo(bo.getFrontAccountBO()));
        BeanUtils.copyProperties(bo, workerDetailVO);
        return workerDetailVO;
    }



    public FrontWorkerBO do2frontBo(WorkerDO workerDO) {
        FrontWorkerBO frontWorkerBO = new FrontWorkerBO();
        if (workerDO!=null){
            BeanUtils.copyProperties(workerDO, frontWorkerBO);
        }
        return frontWorkerBO;
    }

    public PagerResult<AdminWorkerListVO> bo2PagerResultVO(ListRecordTotalBO<AdminWorkerBO> bos, SimplePagerQuery simplePagerQuery) {
        List<AdminWorkerListVO> adminWorkerListVOList = this.bos2vos(bos.getList());
        PagerResult<AdminWorkerListVO> pagerResult = new PagerResult<>(simplePagerQuery);
        pagerResult.setObjects(adminWorkerListVOList);
        pagerResult.setTotal(bos.getTotal());
        return pagerResult;
    }

    public WorkerDBPageQuery query2dbQuery(WorkerListQuery workerListQuery) {
        if (workerListQuery == null) {
            return new WorkerDBPageQuery();
        }
        WorkerDBPageQuery workerDBPageQuery = new WorkerDBPageQuery();
        BeanUtils.copyProperties(workerListQuery, workerDBPageQuery,"startDate","endeDate");
        workerDBPageQuery.setEndDate(workerListQuery.getEnd());
        workerDBPageQuery.setStartDate(workerListQuery.getStart());
        return workerDBPageQuery;
    }

    public List<AdminWorkerBO> dos2bos(List<WorkerDO> dos) {
        List<AdminWorkerBO> adminWorkerBos = new ArrayList<>();
        if (CollectionUtils.isEmpty(dos)) {
            return adminWorkerBos;
        }
        for (WorkerDO workerDO : dos) {
            adminWorkerBos.add(this.do2bo(workerDO));
        }
        return adminWorkerBos;
    }

    public AdminWorkerBO do2bo(WorkerDO workerDO) {
        AdminWorkerBO adminWorkerBO = new AdminWorkerBO();
        if (workerDO == null) {
            return adminWorkerBO;
        }
        BeanUtils.copyProperties(workerDO, adminWorkerBO);
        return adminWorkerBO;
    }

    public WorkerDO param2do(WorkerParam param) {
        WorkerDO worker = new WorkerDO();
        BeanUtils.copyProperties(param, worker);
        UserPrinciple loginUser = LubanSecurityContext.getLoginToken();
        worker.setGmtCreate(System.currentTimeMillis());
        worker.setGmtModified(worker.getGmtCreate());
        worker.setCreateUserId(loginUser.getId());
        worker.setModifiedUserId(loginUser.getId());
        worker.setCreateUserName(loginUser.getUsername());
        worker.setModifiedUserName(loginUser.getUsername());
        return worker;
    }
    private FrontWorkerAccountVO bo2vo(FrontAccountBO bo) {
        FrontWorkerAccountVO vo = new FrontWorkerAccountVO();
        if (bo!=null){
            BeanUtils.copyProperties(bo, vo);
        }
        return vo;
    }
    private List<FrontWorkerCategoryVO> catsBos2catesVos(List<WorkerCategoryBO> list) {
        List<FrontWorkerCategoryVO> categoryVOS=new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            for (WorkerCategoryBO workerCategoryFrontBO : list) {
                categoryVOS.add(bo2vo(workerCategoryFrontBO));
            }
        }
        return categoryVOS;
    }
    private FrontWorkerCategoryVO bo2vo(WorkerCategoryBO bo) {
        FrontWorkerCategoryVO vo = new FrontWorkerCategoryVO();
        if (bo!=null){
            BeanUtils.copyProperties(bo, vo);
        }
        return vo;
    }

    private List<FrontWorkerAreaVO> areaBos2AreaVos(List<WorkerAreaBO> list) {
        List<FrontWorkerAreaVO> areaVOS=new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)){
            for (WorkerAreaBO bo : list) {
                areaVOS.add(bo2vo(bo));
            }
        }
        return areaVOS;
    }

    private FrontWorkerAreaVO bo2vo(WorkerAreaBO bo) {
        FrontWorkerAreaVO vo = new FrontWorkerAreaVO();
        if (bo!=null){
            BeanUtils.copyProperties(bo, vo);
        }
        return vo;
    }

    private List<AdminWorkerListVO> bos2vos(List<AdminWorkerBO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<AdminWorkerListVO> adminWorkerListVOList = new ArrayList<>(list.size());
        for (AdminWorkerBO adminWorkerBo : list) {
            AdminWorkerListVO adminWorkerListVo = this.bo2vo(adminWorkerBo);
            adminWorkerListVOList.add(adminWorkerListVo);
        }
        return adminWorkerListVOList;
    }

    private AdminWorkerListVO bo2vo(AdminWorkerBO bo) {
        AdminWorkerListVO worker = new AdminWorkerListVO();
        if (worker!=null){
            BeanUtils.copyProperties(bo, worker);
        }
        return worker;
    }
}
